package live.jipiao.dao;

import java.sql.SQLException;
import java.util.List;

import live.common.DBHelper;
import live.common.Format;
import live.common.Page;
import live.jipiao.entity.AirOrder;
import live.jipiao.entity.Itinerary;


public class QueryOrders {
	private Format format = Format.getInstance();
	private StringBuffer sql = null;
	/**
	 * 获取用户机票订单信息
	 * @param memberNo
	 * @return
	 * @throws SQLException
	 */
	public List<AirOrder> list_Orders(String memberNo,AirOrder order,Page page) throws SQLException {
		DBHelper dh = DBHelper.getInstance();
		String execSql = "{call pro_Common_PageView(?,?,?,?)}";
		sql = new StringBuffer("SELECT * FROM Plane_Order WHERE 1=1 AND memberNum='").append(memberNo).append("' ");
		if(format.isNotNullOrEmpty(order.getStartDate())){
			sql.append(" AND bookDate>'").append(order.getStartDate()).append("' ");
		}
		if(format.isNotNullOrEmpty(order.getEndDate())){
			sql.append(" AND bookDate<'").append(format.getInstance().returnNextDate(order.getEndDate().toString())).append("' ");
		}
		if(format.isNotNullOrEmpty(order.getOrderStatus())){
			sql.append(" AND orderStatus=").append(2);
		}
		sql.append(" order by bookDate desc ");
		Object[] args = new Object[4];
		args[0]=sql.toString();
		args[1]=page.getPageIndex();
		args[2]=page.getPageSize();
		args[3]="";
		AirOrder item = null;
		dh.setBeanClass(AirOrder.class);
		List orderList = dh.queryRecords(execSql, args);
		page.setRecordCount(dh.returnOutParam(execSql, args));
		dh.close();
		return orderList;
	}
	
	/**
	 * 获取可以申请行程单的订单信息
	 * @param memberNum
	 * @return
	 * @throws SQLException 
	 */
	public List<Itinerary> returnNotApplyItinOrders(String memberNum,AirOrder order,Page page) throws SQLException{
		DBHelper dh = DBHelper.getInstance();
		String execSql = "{call returnNotApplyItinOrders(?)}";
		dh.setBeanClass(Itinerary.class);
		List<Itinerary> itinerayrs = dh.queryRecords(execSql, new Object[]{memberNum});
		dh.close();
		return itinerayrs;
	}
	
	/**
	 * 单个订单查询
	 * @param orderId
	 * @return
	 */
	public AirOrder returnOrderByOrderId(String orderId){
		DBHelper dh = DBHelper.getInstance();
		String sql = new StringBuffer("SELECT * FROM Plane_Order WHERE 1=1 AND orderNo='").append(orderId).append("'").toString();
		dh.setBeanClass(AirOrder.class);
		AirOrder order=null;
		try {
			order = (AirOrder)dh.query(sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//		dh.close();
		return order;
	}
}
